package org.example.multi_sys_consist.service.impl;

import org.example.multi_sys_consist.pojo.entity.DemoUser;
import org.example.multi_sys_consist.service.CurrentService;
import org.springframework.stereotype.Service;

@Service
public class CurrentServiceImpl implements CurrentService {

    private final ThreadLocal<DemoUser> holder = new ThreadLocal<>();
    private final ThreadLocal<Long> timeHolder = new ThreadLocal<>();

    @Override
    public DemoUser currentUser() {
        DemoUser demoUser = holder.get();
        if (demoUser != null) {
            return demoUser;
        }
        //todo 用项目中的方法去替换
        return new DemoUser();
    }

    @Override
    public void setCurrentUser(DemoUser user) {
        holder.set(user);
    }

    @Override
    public void clearAll() {
        holder.remove();
        timeHolder.remove();
    }

    @Override
    public long currentTimeMillis() {
        Long time = timeHolder.get();
        return time != null ? time : System.currentTimeMillis();
    }
}
